<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.trigger()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/events/">事件</a> &gt; <a href="/category/events/event-handler-attachment/">绑定事件处理器</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/trigger/" target="_blank">.trigger()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="trigger1"><h2 class="section-title">
<span class="name">.trigger( eventType [, extraParameters ] )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>
			根据绑定到匹配元素的给定的事件类型执行所有的处理程序和行为。</p>
<ul class="signatures">
<li class="signature" id="trigger-eventType-extraParameters">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.trigger( eventType [, extraParameters ] )</h4>
<ul>
<li>
<div><strong>eventType</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>以后包含JavaScript事件类型的字符串，比如<code>click</code> 或 <code>submit</code>。</div>
</li>
<li>
<div><strong>extraParameters</strong></div>
<div>类型: <a href="/Types/#Array,%20PlainObject">Array, PlainObject</a>
</div>
<div>传递给事件处理程序的额外数组参数。</div>
</li>
</ul>
</li>
<li class="signature" id="trigger-event">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.3/">1.3</a></span>.trigger( event )</h4>
<ul><li>
<div><strong>event</strong></div>
<div>类型: <a href="/Types/#Event">Event</a>
</div>
<div>一个<a href="/category/events/event-object/"><code>jQuery.Event</code></a> 对象.</div>
</li></ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc">
<p>当相应的事件发生时，任何通过<code>.on()</code>或一个快捷方法绑定的事件处理程序将被触发。但是，它们可以用<code>.trigger()</code>方法手动触发。调用 <code>.trigger()</code> 执行处理程序和用户自然的触发该事件，他们的执行顺序时相同的：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">'#foo'</span>).on(<span class="string">'click'</span>, <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>      alert($(<span class="keyword">this</span>).text());</code></div></div><div class="container"><div class="line"><code>    });</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">'#foo'</span>).trigger(<span class="string">'click'</span>);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>从jQuery 1.3开始，<code>.trigger()</code>事件会在DOM树上冒泡;在事件处理程序中返回<code>false</code>或调用事件对象中的<a href="/event.stopPropagation"><code>.stopPropagation()</code></a> 方法可以使事件停止冒泡。尽管 <code>.trigger()</code>  模拟了事件的激活，具备合成的 event 对象，但是它并没有完美的复制自然发生的事件（naturally-occurring event）。</p>
<p>若要触发通过 jQuery 绑定的事件处理函数，而不触发原生的事件，使用<a href="/triggerHandler"><code>.triggerHandler()</code></a> 来代替。 </p>
<p>
			      当我们使用<code>.on()</code>方法定义一个自定义事件类型，<code>.trigger()</code>的第二个参数就有用了。例如，假设我们自定义事件的处理程序绑定到我们的元素而不是内置的click事件，象我们上面那样：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">'#foo'</span>).on(<span class="string">'custom'</span>, <span class="keyword">function</span>(event, param1, param2) {</code></div></div><div class="container"><div class="line"><code>  alert(param1 + <span class="string">"\n"</span> + param2);</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code>$(<span class="string">'#foo'</span>).trigger(<span class="string">'custom'</span>, [<span class="string">'Custom'</span>, <span class="string">'Event'</span>]);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>事件对象始终被传递到事件处理程序的第一个参数，但如果指定了额外的参数调用<code>.trigger()</code> 时，这些参数将被传递给处理程序。要传递多个参数，使用一个数组，如下所示。从jQuery 1.6.2开始，可以通过一个单一的参数，而不使用一个数组。</p>
<p>请注意向该方法中传入的 <code>extraParameters</code> 参数与传入到 <a href="bind.htm">.bind()</a> 方法中的 <code>eventData</code> 参数的区别。它们的机制都是向事件处理函数中传入信息，但是传入 <code>.trigger()</code> 中的 <code>extraParameters</code> 参数是在事件发生时传入的，而传入到 <code>.bind()</code> 中的 <code>eventData</code> 参数要求在进行事件绑定时就要事先计算好。
			         </p>
<p><code>.trigger()</code> 方法可以应用在包裹简单 JavaScript 对象的 jQuery 集合中，类似 pub/sub 机制(观察者机制)。当事件发生时，任何绑定在对象上的处理函数都会被触发。
			         </p>
<div class="warning"><strong>注意:</strong> 对于这两个普通的对象和DOM对象，
			    如果一个触发事件名称和对象的一个属性名称相匹配，
			    如果事件处理程序没有调用<code>event.preventDefault()</code>，jQuery将尝试其他属性作为一个方法来调用。
			    如果这种行为是不希望的，使用<code>.triggerHandler()</code> 来代替。</div>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4>Example: <span class="desc">点击 button #2 时，同时触发 button #1 的点击事件。</span>
</h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">button</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">10</span>px;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">blue;</span></span> <span class="rule"><span class="attribute">font-weight</span>:<span class="value">bold;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">span</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">button</span>&gt;</span>Button #1<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span>&gt;</span>Button #2<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;<span class="title">span</span>&gt;</span>0<span class="tag">&lt;/<span class="title">span</span>&gt;</span> button #1 clicks.<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;<span class="title">span</span>&gt;</span>0<span class="tag">&lt;/<span class="title">span</span>&gt;</span> button #2 clicks.<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"button:first"</span>).click(<span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span></code></div></div><div class="container"><div class="line"><code>update($(<span class="string">"span:first"</span>));</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code>$(<span class="string">"button:last"</span>).click(<span class="function"><span class="keyword">function</span> <span class="params">()</span> {</span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"button:first"</span>).trigger(<span class="string">'click'</span>);</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>update($(<span class="string">"span:last"</span>));</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="function"><span class="keyword">function</span> <span class="title">update</span><span class="params">(j)</span> {</span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> n = parseInt(j.text(), <span class="number">10</span>);</code></div></div><div class="container"><div class="line"><code>j.text(n + <span class="number">1</span>);</code></div></div><div class="container"><div class="line"><code>}</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">若要提交第一个表单但又不想使用 submit() 函数，请尝试如下方法：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">"form:first"</span>).trigger(<span class="string">"submit"</span>)</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-2">
<h4>Example: <span class="desc">若要提交第一个表单但又不想使用 submit() 函数，请尝试如下方法：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> event = jQuery.Event(<span class="string">"submit"</span>);</code></div></div><div class="container"><div class="line"><code>$(<span class="string">"form:first"</span>).trigger(event);</code></div></div><div class="container"><div class="line"><code><span class="keyword">if</span> ( event.isDefaultPrevented() ) {</code></div></div><div class="container"><div class="line"><code><span class="comment">// Perform an action...</span></code></div></div><div class="container"><div class="line"><code>}</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-3">
<h4>Example: <span class="desc">向事件中传入任意的数据：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">"p"</span>).click( <span class="function"><span class="keyword">function</span> <span class="params">(event, a, b)</span> {</span></code></div></div><div class="container"><div class="line"><code><span class="comment">// when a normal click fires, a and b are undefined</span></code></div></div><div class="container"><div class="line"><code><span class="comment">// for a trigger like below a refers to "foo" and b refers to "bar"</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>} ).trigger(<span class="string">"click"</span>, [<span class="string">"foo"</span>, <span class="string">"bar"</span>]);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-4">
<h4>Example: <span class="desc">通过 event 对象，向事件中传入任意的数据：span&gt;
			</span></h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> event = jQuery.Event(<span class="string">"logged"</span>);</code></div></div><div class="container"><div class="line"><code>event.user = <span class="string">"foo"</span>;</code></div></div><div class="container"><div class="line"><code>event.pass = <span class="string">"bar"</span>;</code></div></div><div class="container"><div class="line"><code>$(<span class="string">"body"</span>).trigger(event);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-5">
<h4>Example: <span class="desc">另外一种通过 event 对象传入数据的方法：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">"body"</span>).trigger({</code></div></div><div class="container"><div class="line"><code>type:<span class="string">"logged"</span>,</code></div></div><div class="container"><div class="line"><code>user:<span class="string">"foo"</span>,</code></div></div><div class="container"><div class="line"><code>pass:<span class="string">"bar"</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>